<?php
 Zend_Loader::loadClass("CodesUtility");
 Zend_Loader::loadClass("BaseDBRow");
 class Organization extends BaseDBRow
 {
	const _tablename = "projecte_entity.organizations";
 	protected function setUp()
 	{
 		$this->_tableName = self::_tablename;		
 	}
	
	
	
	public function getAddressByOrgId($orgId,$addressType="",$addressId = -1){
	
		Zend_Loader::loadClass("OrganizationAddress");
		Zend_Loader::loadClass("Address");
		Zend_Loader::loadClass("Country");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("eo.name,eo.trading_name,oa.address_type,oa.org_address_id,ad.address1,if(oa.inactive,'Yes','') inactive_address ");
		$query->addSelect("ad.address2,ad.city,ad.province,ad.country_code,c.country,ad.zip,ad.address_id,oa.address_type ");
		$query->addSelect("if(eo.primary_address_id = oa.org_address_id,'Yes','') primary_address ");
		$query->addFrom($this->_tableName . " as eo ");
		$query->addJoin(" LEFT JOIN ". OrganizationAddress::_tablename . " oa on eo.org_id = oa.org_id ");
		$query->addJoin(" LEFT JOIN ". Address::_tablename . " ad on ad.address_id = oa.address_id ");
		$query->addJoin(" LEFT JOIN ". Country::_tablename . " c on ad.country_code = c.country_code ");
		$query->addWhere("  eo.org_id = ". $orgId );
		if(strlen($addressType) > 0) {
			$query->addWhere(" oa.address_type = '". $addressType . "'");
		}
		
		if($addressId > 0 || $addressId == 0) {
			$query->addWhere(" oa.address_id = ". $addressId );
		}
		
		$db = DBCon::instance();
		// echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
	
	}
	
	public function getContactByOrgId($orgId,$contactId = -1){
		
		Zend_Loader::loadClass("OrganizationContact");
		Zend_Loader::loadClass("Contact");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("c.contact_id, eo.name,eo.trading_name,oc.org_contact_id,c.name contact_name,c.remarks");
		$query->addSelect("c.telephone_no,c.fax_no,c.email,if(oc.inactive,'Yes','') inactive_contact,c.photo_document_id,c.fingerprint_document_id");
		$query->addSelect("c.birth_date, c.license_date, c.blood_group, c.height");
		$query->addSelect("if(eo.primary_contact_id = oc.org_contact_id,'Yes','') primary_contact ");
		$query->addFrom($this->_tableName . " as eo ");
		$query->addJoin(" LEFT JOIN ". OrganizationContact::_tablename . " oc on eo.org_id = oc.org_id ");
		$query->addJoin(" LEFT JOIN ". Contact::_tablename . " c on oc.contact_id = c.contact_id ");
		$query->addWhere(" eo.org_id = ". $orgId );
		if($contactId > 0 || $contactId == 0) {
			$query->addWhere(" oc.contact_id = ". $contactId );
		}
		
		$db = DBCon::instance();
		// echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	public function getPaymentInformationByOrgId($orgId, $paymentId = -1){
		
		Zend_Loader::loadClass("OrganizationPaymentInformation");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("oc.id, eo.org_id, oc.title,oc.content, if(oc.is_active,'Yes','') default_payment");
		$query->addFrom($this->_tableName . " as eo ");
		$query->addJoin(" LEFT JOIN ". OrganizationPaymentInformation::_tablename . " oc on eo.org_id = oc.org_id ");
		$query->addWhere(" eo.org_id = ". $orgId );
		if($paymentId > 0 || $paymentId == 0) {
			$query->addWhere(" oc.id = ". $paymentId );
		}
				
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	public function getActivePaymentInformation($orgId) {
		Zend_Loader::loadClass("OrganizationPaymentInformation");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("oc.id, eo.org_id, oc.title,oc.content, if(oc.is_active,'Yes','') default_payment");
		$query->addFrom($this->_tableName . " as eo ");
		$query->addJoin(" LEFT JOIN ". OrganizationPaymentInformation::_tablename . " oc on eo.org_id = oc.org_id ");
		$query->addWhere(" eo.org_id = ". $orgId );
		$query->addWhere(" oc.is_active  = 1");
				
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		return $result;	
	}
 }
?>